package com.yaozheng.vocationaltraining.fragment;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import android.text.Html;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.iview.experience.IMyExperienceView;
import com.yaozheng.vocationaltraining.iview.integral.IMyIntegralView;
import com.yaozheng.vocationaltraining.iview.integral.IMyLocusView;
import com.yaozheng.vocationaltraining.service.MyIntegralService;
import com.yaozheng.vocationaltraining.service.experience.MyExperienceService;
import com.yaozheng.vocationaltraining.service.impl.MyIntegralServiceImpl;
import com.yaozheng.vocationaltraining.service.impl.experience.MyExperienceServiceImpl;
import com.yaozheng.vocationaltraining.service.impl.integral.MyLocusSerivceImpl;
import com.yaozheng.vocationaltraining.service.integral.MyLocusSerivce;
import com.yaozheng.vocationaltraining.utils.ACache;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.utils.JsonUtils;
import com.yaozheng.vocationaltraining.utils.LogUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.LastLearnStatusProgressView;

import net.sf.json.JSONObject;

/**
 * 我的轨迹
 *
 * @author Administrator
 */
@EFragment(R.layout.fragment_my_locus)
public class MyLocusFragment extends BaseFragment implements IMyLocusView, IMyIntegralView, IMyExperienceView {
    @ViewById
    ImageView userAvatarImage;
    @ViewById
    TextView headTitleText, myLocusUserIntegralExplanationText, homeExplanation;
    //完成的课程数    完成的课时数   完成百分率(完成的课程数/总课程数)   完成课程数超过其他学员的百分率
    @ViewById
    LastLearnStatusProgressView completeCourseCountProgressView, completeHoursProgressView, passRateProgressView, beyondRateProgressView;
    @ViewById
    TextView completeCourseCountTextView, completeHoursTextView, passRateTextView, beyondRateTextView;
    ACache aCache;
    BitmapLoader userBitmapLoader;
    @Bean(MyLocusSerivceImpl.class)
    MyLocusSerivce myLocusSerivce;
    @Bean(MyIntegralServiceImpl.class)
    MyIntegralService myIntegralService;
    @Bean(MyExperienceServiceImpl.class)
    MyExperienceService myExperienceService;
    JSONObject homeJsonObject;
    String userName;
    int experience;
    int integral;

    @AfterViews
    public void initView() {
        headTitleText.setText("我的轨迹");
        userBitmapLoader = new BitmapLoader(Glide.with(this), R.drawable.user_img);
        loadData();
    }

    public void loadData() {
        experience = 0;
        integral = 0;
        aCache = ACache.get(getContext());
        myLocusSerivce.init(this);
        myIntegralService.init(this);
        myExperienceService.init(this);
        String homeDataCache = aCache.getAsString("homeDataCache");
        homeJsonObject = JsonUtils.getJsonObject(homeDataCache);
        String userAvatar = aCache.getAsString("userAvatar");
        userBitmapLoader.displayImage(userAvatar, userAvatarImage);
        userName = aCache.getAsString("userName");

        updateData(null);
        loadIntegralExplanationData();
        JSONObject currUserJsonObject = TypeUtils.getJsonObject(TypeUtils.getJsonObject(homeJsonObject, "data"), "user");
        String city = TypeUtils.getJsonString(currUserJsonObject, "city", "");
        String dealer = TypeUtils.getJsonString(currUserJsonObject, "dealer", "");
        String position = TypeUtils.getJsonString(currUserJsonObject, "position", "");
        StringBuffer explanation = new StringBuffer();

        if (city != null && !"".equals(city)) {
            explanation.append(city);
            explanation.append("*");
        }
        if (dealer != null && !"".equals(dealer)) {
            explanation.append(dealer);
            explanation.append("*");
        }
        if (position != null && !"".equals(position)) {
            explanation.append(position);
        }   // 主页用户说明--成都*建国宝沃*金牌*销售顾问
        homeExplanation.setText(String.valueOf(explanation));

        myLocusSerivce.trackData();
//        myIntegralService.getUserScore();
//        myExperienceService.experienceData();
    }

    public void updateData(JSONObject dataJsonObject) {
        if (dataJsonObject != null) {
            LogUtils.println(dataJsonObject.toString());
        }
        int courseFinishCount = TypeUtils.getJsonInteger(dataJsonObject, "courseFinishCount", 0); // 完成的课程数
        int courseFinishPeriod = TypeUtils.getJsonInteger(dataJsonObject, "courseFinishPeriod", 0); // 完成的课时数
        double courseFinishRate = TypeUtils.getJsonDouble(dataJsonObject, "courseFinishRate", 0); // 完成百分率(完成的课程数/总课程数)
        double beyondRate = TypeUtils.getJsonDouble(dataJsonObject, "beyondRate", 0);// 完成课程数超过其他学员的百分率
        completeCourseCountTextView.setText(String.valueOf(courseFinishCount));
        completeHoursTextView.setText(String.valueOf(courseFinishPeriod));
        passRateTextView.setText(String.valueOf(courseFinishRate));
        beyondRateTextView.setText(String.valueOf(beyondRate));


        completeCourseCountProgressView.showImage();
        completeHoursProgressView.showImage();
        passRateProgressView.showImage();
        beyondRateProgressView.showImage();


        completeCourseCountProgressView.setProgress(100);
        completeHoursProgressView.setProgress(100);
        passRateProgressView.setProgress((float) courseFinishRate);
        beyondRateProgressView.setProgress((float) beyondRate);
    }

    /**
     * 加载积分经验值数据
     */
    public void loadIntegralExplanationData() {
        myLocusUserIntegralExplanationText.setText(getString(
                R.string.myLocusUserIntegralExplanationStr, userName,
                integral + "，经验值：" + experience));


    }

    @UiThread
    @Override
    public void trackDataSuccess(JSONObject jsonObject) {
        cancelLoadDataProgressDialog();
        updateData(TypeUtils.getJsonObject(jsonObject, "data"));
    }


    @UiThread
    @Override
    public void trackDataError(String error) {
        cancelLoadDataProgressDialog();
        alertMessage(error);
    }

    @UiThread
    @Override
    public void userScoreSuccess(JSONObject jsonObject) {
        cancelLoadDataProgressDialog();
        JSONObject scoreJsonObject = TypeUtils.getJsonObject(jsonObject, "data", "score");
        integral = TypeUtils.getJsonInteger(scoreJsonObject, "total", 0);
        loadIntegralExplanationData();
//        myLocusUserIntegralExplanationText.setText(getString(
//                R.string.myLocusUserIntegralExplanationStr, userName,
//                TypeUtils.getJsonInteger(scoreJsonObject, "total", 0)));
    }

    @UiThread
    @Override
    public void userScoreError(String error) {
        cancelLoadDataProgressDialog();
        alertMessage(error);
    }

    @UiThread
    @Override
    public void experienceDataSuccess(JSONObject jsonObject) {
        cancelLoadDataProgressDialog();
        JSONObject experienceJsonObject = TypeUtils.getJsonObject(jsonObject, "data", "experience");
        experience = TypeUtils.getJsonInteger(experienceJsonObject, "experience", 0);
        loadIntegralExplanationData();
    }

    @UiThread
    @Override
    public void experienceDataError(String error) {
        cancelLoadDataProgressDialog();
        alertMessage(error);
    }
}
